package main

import (
	"fmt"
	"math/rand"
	"phoenix/hystrix/windows"
	"sync"
	"time"
)

type RequestMatrix struct {
	Success int
}

func (c *RequestMatrix) New() windows.IncrForCounter {
	return &RequestMatrix{}
}

func (c *RequestMatrix) Incr() {
	c.Success++
}

func (c *RequestMatrix) Sum() int {
	return c.Success
}

func (c *RequestMatrix) Reset() {
	c.Success = 0
}

func NewRequestMatrix() windows.IncrForCounter {
	return &RequestMatrix{}
}

func main() {
	win := windows.NewSlidingWindows(10, 1*time.Second, windows.WithAdder(NewRequestMatrix))
	ticker := time.NewTicker(1 * time.Second)
	//tc := time.NewTicker(100 * time.Millisecond)
	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		defer wg.Done()
		for {
			//<-tc.C
			//fmt.Printf("ticcc")
			str := fmt.Sprintf("%dms", rand.Intn(30))
			t, _ := time.ParseDuration(str)
			time.Sleep(t)
			win.Incr()
		}

	}()

	go func() {
		defer wg.Done()
		for {
			<-ticker.C
			fmt.Println(win.GetBlocks().String())
		}
	}()

	wg.Wait()
}
